package com.six666.medicaldevicetestingsystem.service.impl;

import com.six666.medicaldevicetestingsystem.service.File_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class File_Service_Impl implements File_Service {
    private String uploadDir;

    @Autowired
    public void FileStorageService(Environment env) {
        this.uploadDir = env.getProperty("file.upload-dir");
    }
    @Override
    public Boolean save_file(MultipartFile file,String path,String name) {
        File uploadDir = new File(this.uploadDir+"\\"+path);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        File destFile = new File(this.uploadDir+"\\"+path+"\\" + name);
        try {
            // 保存文件
            file.transferTo(destFile);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean delete_file(String pathname) {
        String filePath = this.uploadDir + "\\" + pathname;

        File file = new File(filePath);
        return file.delete();
    }
}
